﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class CategoryRefDAO//:BaseDAO<RELAXEntities,RelaxCategory>
   {

       #region value
       private readonly RelaxDataContext _context = new RelaxDataContext();
       #endregion

       #region Hàm lấy dữ liệu

       public IQueryable<RelaxCategoryDataContract> GetCategoryDataContracts()
       {
           var lReturn = (from c in _context.CATEGORY_REFs
                         join v in _context.CATEGORY_VALUEs on c.id equals v.categoryid
                         select new RelaxCategoryDataContract
                         {
                             Id = Convert.ToInt32(c.id),
                             Name = v.name,
                             Tag = v.tag,
                             Keyword = v.keyword,
                             Decription = v.decription,
                             Type = Convert.ToInt32(c.typeid),
                             Languagecode = Convert.ToInt32(v.languagecode),
                             Active = Convert.ToInt32(c.active),
                             Ord = Convert.ToInt32(c.ord)
                         }).Distinct();
           return lReturn;
       }
       public IQueryable<RelaxCategoryDataContract> GetCategoryDataContract()
       {
           var lReturn = from c in _context.CATEGORY_REFs
                         select new RelaxCategoryDataContract
                         {
                             Id = Convert.ToInt32(c.id),
                             Type = Convert.ToInt32(c.typeid),
                            Active = Convert.ToInt32(c.active),
                            Ord = Convert.ToInt32(c.ord)
                         };
           return lReturn;
       }
       #endregion
       #region Add
       public void Add(CATEGORY_REF category)
       {
           _context.CATEGORY_REFs.InsertOnSubmit(category);
           _context.SubmitChanges();

       }
       #endregion
       #region Update

       public void Update(CATEGORY_REF category)
       {
           var first = _context.CATEGORY_REFs.FirstOrDefault(c => c.id == category.id);
           if (first != null)
           {
               first.ord = category.ord;
               first.active = category.active;
               first.typeid = category.typeid;
             
               _context.SubmitChanges();
           }
       }
      
       #endregion

       #region Delete

       public void Delete(long iId)
       {
           var first = _context.CATEGORY_REFs.FirstOrDefault(c => c.id == iId);
           if (first != null)
           {
               _context.CATEGORY_REFs.DeleteOnSubmit(first);
               _context.SubmitChanges();
           }
       }
       #endregion

   }
}
